package com.meatball.dji.common.mqtt.router

import com.meatball.dji.common.mqtt.properties.CloudApiProperties
import org.eclipse.paho.client.mqttv3.MqttClient
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Component

/**
 * TODO 路由类
 *
 * @property cloudApiProperties
 *
 * @author 张翔宇
 * @since 2024年4月22日
 */
@Component
class MqttMessageRouter(
    private val cloudApiProperties: CloudApiProperties
) {
    companion object {
        private val logger = LoggerFactory.getLogger(MqttMessageRouter::class.java)
    }
    fun routeMessage(methodName: String, message: String, mqttClient: MqttClient) {
        val handler = MessageHandlerFactory(mqttClient, cloudApiProperties).createHandler(methodName)
        if (handler != null) {
            try {
                handler.handle(message)
            } catch (e: Exception) {
                println("错误的路由信息: $message")
                logger.error("")
                e.printStackTrace()
            }
        } else {
            logger.error("未找到Handler对应的方法: $methodName")
        }
    }

    fun osdMessage(topic: String, message: String, mqttClient: MqttClient) {
        routeMessage("osd", message, mqttClient)
    }
}